/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.snda.lidejia.hivetotalsort.type;

import org.apache.hadoop.io.WritableComparator;
import org.apache.hadoop.io.WritableUtils;

/**
 *
 * @author lidejia
 */
public class HiveSortKeyComparator extends WritableComparator {

    private static final HiveTypeComparator HIVE_TYPE_CMP = new HiveTypeComparator();

    public HiveSortKeyComparator() {
        super(HiveSortKey.class);
    }

    @Override
    public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
        int keySize = readInt(b1, s1);
        s1 += 4;
        s2 += 4;
        int cmp = 0;
        for (int i = 0; i < keySize; i++) {
            int hiveTypeSize1 = readInt(b1, s1);
            int hiveTypeSize2 = readInt(b2, s2);
            cmp = HIVE_TYPE_CMP.compare(b1, s1, hiveTypeSize1, b2, s2, hiveTypeSize2);
            if (cmp != 0) {
                break;
            }
            s1 += hiveTypeSize1;
            s2 += hiveTypeSize2;
        }
        return cmp;
    }
}
